package com.ruoyi.web.controller.smy;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.exceptions.ValidateException;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.smy.MallGoodsChannel;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.smy.IMallGoodsChannelService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 商品类目
 * @classname MallGoodsChannelController
 * @author ZhanCheng.Li
 * @date 2022/10/24 15:16
 * @version 1.0.0
 */
@RestController
@RequestMapping(value = "/smy/goodsChannel")
public class MallGoodsChannelController extends BaseController {
	@Resource
	private IMallGoodsChannelService mallGoodsChannelService;

	@Value("${microApp.imageUrl}")
	private String microImageUrl;

	@PreAuthorize("@ss.hasPermi('system:channel:list')")
	@GetMapping("/list")
	public TableDataInfo list(MallGoodsChannel mallGoodsChannel) {
		startPage();
		List<MallGoodsChannel> list = mallGoodsChannelService.selectMallGoodsChannelList(mallGoodsChannel);
		return getDataTable(list);
	}

	@PreAuthorize("@ss.hasPermi('system:channel:export')")
	@Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, MallGoodsChannel mallGoodsChannel) {
		List<MallGoodsChannel> list = mallGoodsChannelService.selectMallGoodsChannelList(mallGoodsChannel);
		ExcelUtil<MallGoodsChannel> util = new ExcelUtil<MallGoodsChannel>(MallGoodsChannel.class);
		util.exportExcel(response, list, "【请填写功能名称】数据");
	}

	@PreAuthorize("@ss.hasPermi('system:channel:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(mallGoodsChannelService.selectMallGoodsChannelById(id));
	}

	@PreAuthorize("@ss.hasPermi('system:channel:add')")
	@Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MallGoodsChannel mallGoodsChannel) {
		return toAjax(mallGoodsChannelService.insertMallGoodsChannel(mallGoodsChannel));
	}

	@PreAuthorize("@ss.hasPermi('system:channel:edit')")
	@Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MallGoodsChannel mallGoodsChannel) {
		return toAjax(mallGoodsChannelService.updateMallGoodsChannel(mallGoodsChannel));
	}

	@PreAuthorize("@ss.hasPermi('smy:goodsChannel:remove')")
	@Log(title = "删除商品类目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
	public AjaxResult remove(@PathVariable Long id) {
		try {
			MallGoodsChannel channel = mallGoodsChannelService.selectMallGoodsChannelById(id);
			if(Objects.nonNull(channel)){
				//查询下级商品类目中是否还有在使用中的
				MallGoodsChannel queryDto = new MallGoodsChannel();
				queryDto.setDeletestatus(0L);
				queryDto.setParent(channel.getId());
				List<MallGoodsChannel> childrenList = mallGoodsChannelService.selectMallGoodsChannelList(queryDto);
				if(CollectionUtil.isNotEmpty(childrenList)){
					throw new ValidateException("操作的类目仍在使用中，不可直接删除！");
				}
				channel.setDeletestatus(1L);
				mallGoodsChannelService.updateMallGoodsChannel(channel);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("删除商品类目异常:{}",e.getMessage());
			return AjaxResult.error(e.getMessage());
		}
		return AjaxResult.success();
	}

	/**
	 * 查询所有的商品类目
	 * @Date 2022/10/24 15:32
	 * @author ZhanCheng.Li
	 * @return
	 */
	@GetMapping(value = "getAllGoodsChannel")
	public AjaxResult getAllGoodsChannel() {
		return AjaxResult.success(mallGoodsChannelService.getAllGoodsChannel(microImageUrl));
	}

}
